//
//  TSRecordAlertView.swift
//  TSProfitProject
//
//  Created by TmmmS on 2019/11/22.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

class TSRecordAlertView: UIView {

    var container : UIView!
    var itemContainer : UIView!
    var investItem : TSRecordAlertItemView!
    var periodItem : TSRecordAlertItemView!
    var gainItem : TSRecordAlertItemView!
    var dateItem : TSRecordAlertItemView!
    var stateItem : TSRecordAlertItemView!
    var openTimeItem : TSRecordAlertItemView!
    var resultItem : TSRecordAlertItemView!
    var numberItem : TSRecordAlertItemView!
    var investLabel : UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.init(white: 0, alpha: 0.8)
        configViews()
        drawBorderLine()
    }
    
    @objc func dismissAction() {
        self.isHidden = true
    }
    
    func setupView(model:TSRecordModel) {
        
        investItem.setupItem(value: "购买金额：\(model.money)")
        periodItem.setupItem(value: "期号：\(model.qihao)")
        gainItem.setupItem(value: String.init(format: "中奖金额：%.2f", model.shouyi))
        let date = Date.timeStampToString(timeStamp: model.addtime)
        dateItem.setupItem(value: "投注时间："+date)
        let openTime = Date.timeStampToString(timeStamp: model.opentime)
        openTimeItem.setupItem(value: "开奖时间："+openTime)
        resultItem.setupItem(value: "购买盈亏："+(model.yingkui ?? "0"))
        numberItem.setupItem(value: "开奖号码：\(model.jianghao)")
        investLabel.text = "\(model.one)，\(model.two)，\(model.three)，\(model.four)"
        
        switch model.status {
        case 0:
            stateItem.setupItem(value: "订单状态：未开奖")
        case 1:
            stateItem.setupItem(value: "订单状态：中奖")
        case 2:
            stateItem.setupItem(value: "订单状态：未中奖")
        case 3:
            stateItem.setupItem(value: "订单状态：撤单")
        default:
            break
        }
        
    }
    
    func configViews() {
        
        container = UIView.init()
        container.backgroundColor = WHITE_COLOR
        container.layer.cornerRadius = 10
        container.layer.masksToBounds = true
        addSubview(container)
        container.snp.makeConstraints { (make) in
            make.centerX.centerY.equalToSuperview()
            make.width.equalTo(SCREEN_WIDTH - 2*15)
        }
        
        let titleLabel = UILabel.init()
        titleLabel.text = "投注信息"
        titleLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        titleLabel.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.top.equalTo(10)
        }
        
        itemContainer = UIView.init()
        itemContainer.backgroundColor = WHITE_COLOR
        container.addSubview(itemContainer)
        itemContainer.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(10)
            make.top.equalTo(titleLabel.snp_bottom).offset(10)
            make.height.equalTo(105)
        }
        
        investItem = TSRecordAlertItemView.init()
        itemContainer.addSubview(investItem)
        
        gainItem = TSRecordAlertItemView.init()
        itemContainer.addSubview(gainItem)
        
        stateItem = TSRecordAlertItemView.init()
        itemContainer.addSubview(stateItem)
        
        let leftArray = [investItem, gainItem, stateItem]
        leftArray.snp.distributeViewsAlong(axisType: .vertical, fixedItemLength: 35, leadSpacing: 0, tailSpacing: 0)
        leftArray.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.4)
        }
        
        periodItem = TSRecordAlertItemView.init()
        itemContainer.addSubview(periodItem)
        
        dateItem = TSRecordAlertItemView.init()
        itemContainer.addSubview(dateItem)
        
        openTimeItem = TSRecordAlertItemView.init()
        itemContainer.addSubview(openTimeItem)
        
        let rightArray = [periodItem, dateItem, openTimeItem]
        rightArray.snp.distributeViewsAlong(axisType: .vertical, fixedItemLength: 35, leadSpacing: 0, tailSpacing: 0)
        rightArray.snp.makeConstraints { (make) in
            make.right.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.6)
        }
                
        resultItem = TSRecordAlertItemView.init()
        container.addSubview(resultItem)
        resultItem.snp.makeConstraints { (make) in
            make.left.equalTo(investItem)
            make.right.equalTo(periodItem)
            make.top.equalTo(itemContainer.snp_bottom)
            make.height.equalTo(35)
            
        }
        
        numberItem = TSRecordAlertItemView.init()
        container.addSubview(numberItem)
        numberItem.snp.makeConstraints { (make) in
            make.left.right.equalTo(resultItem)
            make.top.equalTo(resultItem.snp_bottom)
            make.height.equalTo(resultItem)
            
        }
        
        let infoLabel = UILabel.init()
        infoLabel.text = "投注内容"
        infoLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        infoLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        container.addSubview(infoLabel)
        infoLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(numberItem.snp_bottom).offset(10)
        }
        
        investLabel = UILabel.init()
        investLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        investLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        container.addSubview(investLabel)
        investLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(infoLabel.snp_bottom).offset(6)
        }
        
        let dismissBtn = UIButton.init(type: .system)
        dismissBtn.setTitle("关闭", for: .normal)
        dismissBtn.setTitleColor(WHITE_COLOR, for: .normal)
        dismissBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        dismissBtn.backgroundColor = BASE_PURPLE_COLOR
        dismissBtn.layer.cornerRadius = 20
        dismissBtn.layer.masksToBounds = true
        dismissBtn.addTarget(self, action: #selector(dismissAction), for: .touchUpInside)
        container.addSubview(dismissBtn)
        dismissBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(investLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: (SCREEN_WIDTH - 2*15)*0.3, height: 40))
            
            make.bottom.equalTo(-10)
        }
    }
    
    func drawBorderLine() {
        
        let topBorderLine = drawLine()
        container.addSubview(topBorderLine)
        topBorderLine.snp.makeConstraints { (make) in
            make.left.right.top.equalTo(itemContainer)
            make.height.equalTo(1)
        }
        
        let leftBorderLine = drawLine()
        container.addSubview(leftBorderLine)
        leftBorderLine.snp.makeConstraints { (make) in
            make.left.top.equalTo(itemContainer)
            make.bottom.equalTo(numberItem)
            make.width.equalTo(1)
        }
        
        let rightBorderLine = drawLine()
        container.addSubview(rightBorderLine)
        rightBorderLine.snp.makeConstraints { (make) in
            make.right.top.equalTo(itemContainer)
            make.bottom.equalTo(numberItem)
            make.width.equalTo(1)
        }
        
        let bottomBorderLine = drawLine()
        container.addSubview(bottomBorderLine)
        bottomBorderLine.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(numberItem)
            make.height.equalTo(1)
        }
        
        let line1 = drawLine()
        container.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.left.right.equalTo(topBorderLine)
            make.top.equalTo(itemContainer).offset(35)
            make.height.equalTo(1)
        }
        
        let line2 = drawLine()
        container.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.left.right.equalTo(topBorderLine)
            make.top.equalTo(itemContainer).offset(35*2)
            make.height.equalTo(1)
        }
        
        let line3 = drawLine()
        container.addSubview(line3)
        line3.snp.makeConstraints { (make) in
            make.left.right.equalTo(topBorderLine)
            make.top.equalTo(itemContainer).offset(35*3)
            make.height.equalTo(1)
        }
        
        let line4 = drawLine()
        container.addSubview(line4)
        line4.snp.makeConstraints { (make) in
            make.left.right.equalTo(topBorderLine)
            make.top.equalTo(itemContainer.snp_bottom).offset(35)
            make.height.equalTo(1)
        }
        
        let line5 = drawLine()
        container.addSubview(line5)
        line5.snp.makeConstraints { (make) in
            make.top.equalTo(itemContainer)
            make.left.equalTo(investItem.snp_right)
            make.size.equalTo(CGSize(width: 1, height: 35*3))
        }
    }
    
    func drawLine() -> UIView {
        let line = UIView.init()
        line.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#DDDDDD")
        return line;
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class TSRecordAlertItemView : UIView {
    
    private var itemLabel : UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupItem(value:String) {
        itemLabel.text = value
    }
    
    func configViews() {
        
        itemLabel = UILabel.init()
        itemLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        itemLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        itemLabel.textAlignment = .center
        itemLabel.adjustsFontSizeToFitWidth = true
        itemLabel.minimumScaleFactor = 0.6
        addSubview(itemLabel)
        itemLabel.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
//    required init?(coder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
}
